package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.louwowo.cache.key.RedisKeys;
import cn.wolfcode.louwowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.louwowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("commends",
                strategyCommendService.getTop5());
        //查询出海外、国内排名zset中的所有vo对象
        List<StrategyStatisVO> voList = strategyStatisRedisService.getVoByZsetKey(RedisKeys.STRATEGY_STATIS_SORT.getPrefit(), 0, -1);
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : voList) {
            if(vo.isIsabroad()){
                if(abroadCds.size() < 10){  //如果是海外，并且集合内少于10个，就存入
                    abroadCds.add(vo);
                }
            }else {
                if(unabroadCds.size() < 10){    //如果是国内，并且集合内少于10个，就存入
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds", abroadCds);
        model.addAttribute("unabroadCds", unabroadCds);
        //查询出热门攻略排名zset中的前10个
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getVoByZsetKey(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefit(),
                0, 9);
        model.addAttribute("hotCds", hotCds);
        List<StatisVO> chinas = strategyTemplateSearchService.queryCondition(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);
        model.addAttribute("chinas",chinas);
        List<StatisVO> abroads = strategyTemplateSearchService.queryCondition(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads",abroads);
        List<StatisVO> themes = strategyTemplateSearchService.queryCondition(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes",themes);
        List<Map<String, Object>> themeCds = strategyTemplateSearchService.queryThemeCommend();
        model.addAttribute("themeCds",themeCds);
        return "strategy/index";
    }

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") StrategyDetailQuery qo){
        model.addAttribute("tags",strategyTagService.list());
        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        model.addAttribute("toasts",destinationService.getToasts(qo.getDestId()));
        model.addAttribute("dest",destinationService.getDestById(qo.getDestId()));
        return "strategy/list";
    }

    @RequestMapping("detail")
    public String detail(Model model,Long id,@UserParam UserInfo userInfo){
        model.addAttribute("detail",strategyDetailService.get(id));
        strategyStatisRedisService.viewNumIncrease(id,1);
        model.addAttribute("vo", strategyStatisRedisService.getStatisVo(id));
        if(userInfo != null){
            //用户登陆时，判断该攻略是否被当前用户收藏，用于收藏样式的回显
            model.addAttribute("isFavor",strategyStatisRedisService.isFavor(userInfo.getId(),id));
        }
        strategyStatisRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefit(),
                RedisKeys.STRATEGY_STATIS_VO.join(id.toString()), 1);
        strategyStatisRedisService.addScore(RedisKeys.STRATEGY_STATIS_CHINA_HOT_SORT.getPrefit(),
                RedisKeys.STRATEGY_STATIS_VO.join(id.toString()), 1);
        return "strategy/detail";
    }

    @RequestMapping("commentAdd")
    @ResponseBody
    public AjaxResult commentAdd(Model model, StrategyComment strategyComment, @UserParam UserInfo userInfo){
        //先判断是否登陆，没登陆提示用户登陆
        if(userInfo == null){
            return AjaxResult.UNLOGIN();
        }
        //把用户的信息设置到攻略评论对象中
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setUsername(userInfo.getNickname());
        strategyComment.setCity(userInfo.getCity());
        strategyComment.setLevel(userInfo.getLevel());
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.saveOrUpdate(strategyComment);
        //攻略的评论数加一
        strategyStatisRedisService.replynumIncrease(strategyComment.getDetailId(),1);
        StrategyStatisVO vo = strategyStatisRedisService.getStatisVo(strategyComment.getDetailId());
        strategyStatisRedisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefit(),
                RedisKeys.STRATEGY_STATIS_VO.join(strategyComment.getDetailId().toString()), 1);
        return new AjaxResult().getData(vo.getReplynum());
    }

    @RequestMapping("comment")
    public String comment(Model model,StrategyCommentQuery qo){
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy /commentTpl";
    }

    @RequestMapping("commentThumbUp")
    @ResponseBody
    public AjaxResult commentThumbUp(String toid,Long fromid){
        strategyCommentService.commentThumbUp(toid,fromid);
        return new AjaxResult();
    }

    @RequestMapping("favor")
    @ResponseBody
    public AjaxResult favor(Long sid,@UserParam UserInfo userInfo){
        //先判断是否登陆
        if(userInfo == null){
            return AjaxResult.UNLOGIN();
        }
        boolean b = strategyStatisRedisService.favor(userInfo.getId(),sid);
        strategyStatisRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefit(),
                RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()),
                b? 1: -1);
        //true表示是收藏操作，false表示取消收藏
        return new AjaxResult(b,"").getData(strategyStatisRedisService.getStatisVo(sid));
    }

    @RequestMapping("strategyThumbup")
    @ResponseBody
    public AjaxResult strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        //先判断是否登陆
        if(userInfo == null){
            return AjaxResult.UNLOGIN();
        }
        boolean b = strategyStatisRedisService.thumbup(sid, userInfo.getId());
        if(!b){
            strategyStatisRedisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefit(),
                    RedisKeys.STRATEGY_STATIS_VO.join(sid.toString()), 1);
        }
        //true表示是已顶过了，false表示今天还没顶
        return new AjaxResult(!b,"").getData(strategyStatisRedisService.getStatisVo(sid));
    }

    @RequestMapping("shareAdd")
    @ResponseBody
    public AjaxResult shareAdd(Long sid){
        strategyStatisRedisService.shareNumIncrease(sid, 1);
        return new AjaxResult().getData(strategyStatisRedisService.getStatisVo(sid));
    }

    @RequestMapping("searchPage")
    public String searchPage(Model model,@ModelAttribute ("qo") SearchQueryObject qo){
        model.addAttribute("page",strategyTemplateSearchService.query(qo));
        return "strategy/searchPageTpl";
    }
}
